//
//  TranstionCountView.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/8/23.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "TranstionCountView.h"
#import <UIImageView+WebCache.h>
#import "MBProgressHUD+MJ.h"

@interface TranstionCountView()
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *futureLab;
@property (weak, nonatomic) IBOutlet UILabel *priceLab;
@property (weak, nonatomic) IBOutlet UILabel *stockLab;
@property (weak, nonatomic) IBOutlet UITextField *countField;
@property (weak, nonatomic) IBOutlet UIButton *minusBtn;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;


@end
@implementation TranstionCountView

+ (instancetype)shareTranstionView
{
    return [[[NSBundle mainBundle] loadNibNamed:@"TranstionCountView" owner:self options:nil] lastObject];
}
- (void)doAnimation
{
    self.bottomConstraint.constant = 0;
    [UIView animateWithDuration:0.3 animations:^{
        [self layoutIfNeeded];
    }];
}
- (void)awakeFromNib
{
    [super awakeFromNib];
    self.minusBtn.layer.borderColor = [UIColor grayColor].CGColor;
    self.countField.layer.borderColor = [UIColor grayColor].CGColor;
    self.addBtn.layer.borderColor = [UIColor grayColor].CGColor;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismiss];
}
- (IBAction)countClick:(UIButton *)btn
{
    NSInteger count = [self.countField.text integerValue];
    if (btn.tag == 0) {
        if (count == 1) {
            [MBProgressHUD showError:@"购买数量不能少于1件"];
            return;
        }
        count--;
    }else{
        count++;
    }
    self.countField.text = [NSString stringWithFormat:@"%zd",count];
}
- (IBAction)sureClick
{
    if ([self.delegate respondsToSelector:@selector(TranstionCountViewSureClick:)]) {
        [self.delegate TranstionCountViewSureClick:[self.countField.text integerValue]];
    }
    [self dismiss];
}
- (void)dismiss
{
    self.bottomConstraint.constant = -220;
    [UIView animateWithDuration:0.3 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (void)setGooodsModel:(AddGoodsModel *)gooodsModel
{
    _gooodsModel = gooodsModel;
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:gooodsModel.thumbnailUrl] placeholderImage:GetImage(@"default_goods_image")];
    //self.futureLab.text = gooodsModel.productName;
    //调整行间距
    NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:gooodsModel.name];
    NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:5];
    [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [gooodsModel.name length])];
    [self.futureLab setAttributedText:attributedString1];
    
    self.priceLab.text = [NSString stringWithFormat:@"¥%.2f",gooodsModel.price];
    self.stockLab.text = nil;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    CGPoint coverPoint = [self convertPoint:point toView:self.contentView];
    if ([self.contentView pointInside:coverPoint withEvent:event]) {
        CGPoint minusPoint = [self convertPoint:point toView:self.minusBtn];
        CGPoint addPoint = [self convertPoint:point toView:self.addBtn];
        CGPoint surePoint = [self convertPoint:point toView:self.sureBtn];
        if ([self.minusBtn pointInside:minusPoint withEvent:event]) {
            return self.minusBtn;
        }else if ([self.addBtn pointInside:addPoint withEvent:event]){
            return self.addBtn;
        }else if ([self.sureBtn pointInside:surePoint withEvent:event]){
            return self.sureBtn;
        }
        return nil;
    }
    return [super hitTest:point withEvent:event];
}


@end
